package com.museum.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.museum.common.Result;
import com.museum.entity.Exhibition;
import com.museum.service.ExhibitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/exhibition")
public class ExhibitionController {

    @Autowired
    private ExhibitionService exhibitionService;

    @GetMapping("/list")
    public Result<Page<Exhibition>> list(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "20") Integer size,
            @RequestParam(value = "status", required = false) Integer status) {
        Page<Exhibition> pageParam = new Page<>(page, size);
        Page<Exhibition> result = exhibitionService.pageList(pageParam, status);
        return Result.success(result);
    }

    @GetMapping("/detail/{id}")
    public Result<Exhibition> detail(@PathVariable Long id) {
        Exhibition exhibition = exhibitionService.getById(id);
        if (exhibition == null) {
            return Result.error("展览不存在");
        }
        return Result.success(exhibition);
    }

    @PostMapping("/add")
    public Result<Exhibition> add(@RequestBody Exhibition exhibition) {
        exhibitionService.save(exhibition);
        return Result.success(exhibition);
    }

    @PutMapping("/update")
    public Result<Exhibition> update(@RequestBody Exhibition exhibition) {
        if (exhibition.getId() == null) {
            return Result.error("展览ID不能为空");
        }
        exhibitionService.updateById(exhibition);
        return Result.success(exhibition);
    }

    @PutMapping("/status/{id}")
    public Result<Boolean> updateStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        boolean success = exhibitionService.updateStatus(id, status);
        if (!success) {
            return Result.error("操作失败");
        }
        return Result.success(true);
    }

    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = exhibitionService.removeById(id);
        if (!success) {
            return Result.error("删除失败");
        }
        return Result.success(true);
    }
} 